package com.zipking.cloud.springbootmybatis.util;

import cn.hutool.core.collection.CollectionUtil;
import com.google.common.collect.Lists;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 拆分结合工具类
 *
 * @author shiwen
 */
public class SplitListUtils {
    /**
     * List按指定长度拆分
     *
     * @param list 要拆分的list
     * @param size 拆分的长度
     * @param <T>  泛型
     * @return
     */
    public static <T> List<List<T>> partition(final List<T> list, final int size) {
        // 非法处理
        if (CollectionUtil.isEmpty(list) || size <= 0) {
            return Lists.newArrayList();
        }

        List<List<T>> result = Lists.newArrayList();
        // 迭代器
        Iterator<T> it = list.iterator();
        // List片段存储器
        List<T> subList = null;
        while (it.hasNext()) {
            if (subList == null) {
                subList = new ArrayList<>();
            }
            T t = it.next();
            subList.add(t);
            if (subList.size() == size) {
                result.add(subList);
                subList = null;
            }
        }
        // 补充最后一页
        if (subList != null) {
            result.add(subList);
        }
        return result;
    }

    public static void main(String[] args) {
       /* SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      //  log.info(sdf.format(new Date()));
        final int[] currentNum = {0};
        // 调用list拆分方法 拆分数据
        //List<List> groupList = ToolUtil.partition(totalList, 100);
        // 创建线程池
        ExecutorService executorService = Executors.newFixedThreadPool(groupList.size());
        // 声明线程计数器 记录单个任务的执行次数
        CountDownLatch countDownLatch = new CountDownLatch(groupList.size());
        // 遍历处理拆分的list数据
        for (int i = 0; i < groupList.size(); i++) {
            int finalI = i;
            executorService.execute(() -> {

                countDownLatch.countDown();
            });
        }
        countDownLatch.await();
        //关闭线程池
        executorService.shutdown();
        log.info(sdf.format(new Date()));*/


       /* List<Product> products = getBatchListTest();
        List<List<Product>> allList = Lists.partition(products, 100);
        int batchNum = allList.size();
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        Executor threadConfigExecutor = threadConfig.getExecutor();
        List<CompletableFuture> results = new ArrayList<>();
        for (List<Product> batchProducts :allList){
            CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
                productService.batchDealList(batchProducts);
                return "";
            }, threadConfigExecutor);
            results.add(future);
        }
        CompletableFuture.allOf(results.toArray(results.toArray(new CompletableFuture[batchNum]))).join();
        stopWatch.stop();
        System.out.println("总用时"+stopWatch.getTotalTimeMillis()+"毫秒");
*/


    }
}
